package com.longdaji.decoration.ui.login.forgetPassword;

import android.text.TextUtils;
import android.util.Log;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.CommonResult;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.data.network.HttpResponse;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/12 0012.
 */

public class ForgetPasswordPresenter extends RxPresenter<ForgetPasswordContract.IView> implements ForgetPasswordContract.IPresenter {
    private DataManager mDataManager;

    private static final String TAG = "ForgetPasswordPresenter";

    @Inject
    public ForgetPasswordPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
    }

    @Override
    public void onGetVerificationCodeClick(final String telephone) {
        if (11 != telephone.length()) {
            mView.showIllegalTelephone();
            return;
        }
        addSubscribe(mDataManager.doTelephoneVerifyApi(null, telephone)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        // TODO: 2018/3/17 0017 当前号码已注册，才可以获取获取验证码
                        if (TextUtils.equals(Constants.OK, httpResponse.getMessage())) {
                            mView.showTelephoneVerifyFailed();
                            return;
                        }
                        Log.d("mytest", "doTelephoneVerifyApi success : " + httpResponse.getMessage());
                        mView.calculateTime();
                        sendVerificationRequest(telephone);
                    }

                    @Override
                    public void onFailed(String msg) {
                        // TODO: 2018/3/17 0017 当前号码未注册
                        LogUtil.d("mytest", "failed onGetVerificationCodeClick, msg: " + msg);
                    }
                })

        );
    }

    private void sendVerificationRequest(String telephone) {
        addSubscribe(mDataManager.doVerificationRequest(telephone, null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        Log.d("mytest", "sendVerificationRequest success");
                        mView.showSendVerification(true);
                    }

                    @Override
                    public void onFailed(String msg) {
                        Log.d("mytest", "msg : " + msg);
                        mView.showSendVerification(false);
                    }
                }));
    }

    @Override
    public void onConfirmClick(final String telephone, final String password, String code) {
        addSubscribe(mDataManager.doVerificationResult(telephone, code)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed doVerificationResult ");
                        doForgetPassword(telephone, password);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed doVerificationResult, msg: " + msg);
                        mView.showCodeVerifyFailed();
                    }
                }));
    }

    private void doForgetPassword(final String telephone, final String password) {
        addSubscribe(mDataManager.doForgetPasswordApi(telephone, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(new CommonSubscriber<HttpResponse>() {
                    @Override
                    public void onSuccess(HttpResponse httpResponse) {
                        LogUtil.d("succeed doForgetPassword: " + httpResponse.getMessage());
//                        mView.goPasswordLogin();
                        doLoginByNewPassword(telephone, password, null);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d("failed doForgetPassword, msg: " + msg);
                    }
                }));
    }

    private void doLoginByNewPassword(String account, String password, String loginTime) {
        addSubscribe(mDataManager.doPasswordLoginApi(account, password, loginTime)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<Account>handleResult())
                .subscribeWith(new CommonSubscriber<Account>() {
                    @Override
                    public void onSuccess(Account account) {
                        LogUtil.d(TAG, "login succeed : msg : " + account.toString());
                        Account.setInstance(account);
                        initAccount();
                        mView.goMainByUsername();
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.d(TAG, "login failed : msg : " + msg);
                    }
                })
        );
    }

    // TODO: 2018/3/28 0028 只需要在注册时候传入0和1，后面都可以设置为男和女
    private void initAccount() {
        if (TextUtils.equals("0", Account.getInstance().getGender())) {
            Account.getInstance().setGender("男");
        } else if (TextUtils.equals("1", Account.getInstance().getGender())) {
            Account.getInstance().setGender("女");
        }
    }

    @Override
    public void onHidePasswordClick(boolean isPasswordHidden) {
        if (isPasswordHidden) {
            mView.showHidePassword(false);
        } else {
            mView.showHidePassword(true);
        }
    }
}
